﻿Public Class FormInto
    Private WithEvents UcDigitalKeyboard1 As ucDigitalKeyboard
    Dim dt As DataTable
    Dim dtFactory As New DataTable
    Dim objFactory0utsourceMst As New clsFactory0utsourceMst
    Dim objpw_ck As New clsPWck
    Dim objAutoNumber As New clsAutoNumber
    Public usr As String
    Private Sub FormInto_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        initdt()
        setForm()
        'setEmptyLOC()
    End Sub

    Private Sub initdt()
        dt = New DataTable
        dt.Columns.Add("batch_no")
        dt.Columns.Add("oper_type")
        dt.Columns.Add("type_value")
        dt.Columns.Add("material_code")
        dt.Columns.Add("pw_date")
        dt.Columns.Add("into_date")
        dt.Columns.Add("into_num")
        dt.Columns.Add("assist_marker")
        dgv.DataSource = dt
    End Sub

    Private Sub setForm()
        setMstComboBox(Me.cmb_type, "02")
        cmb_type.SelectedIndex = 0
        dtFactory = objFactory0utsourceMst.getList("1")
        If dtFactory.Rows.Count > 0 Then
            Dim dr As DataRow() = dtFactory.Select("product<>'jnj'")
            cmb_factory.Items.Clear()
            For index = 0 To dr.Length - 1
                cmb_factory.Items.Add(dr(index)("name"))
            Next
            If cmb_factory.Items.Count > 0 Then
                cmb_factory.SelectedIndex = 0
            End If
        End If
        rdo_gjb.Checked = True
        pwdt.Text = Now().ToShortDateString()
        rkdt.Text = Now().ToShortDateString()
        rk_num.Value = 0
        txtBatchNo.Focus()
    End Sub

    Private Sub rdo_gjb_CheckedChanged(sender As Object, e As EventArgs) Handles rdo_gjb.CheckedChanged
        If rdo_gjb.Checked Then
            setMstComboBox(Me.cmb_type, "02")
        End If
    End Sub

    Private Sub rdo_flb_CheckedChanged(sender As Object, e As EventArgs) Handles rdo_flb.CheckedChanged
        If rdo_flb.Checked Then
            setMstComboBox(Me.cmb_type, "01")
        End If
    End Sub

    Private Sub cmb_type_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmb_type.SelectedIndexChanged
        txtSize.Text = cmb_type.SelectedValue.ToString()
        '生成批次号
        Dim batch_no As String = Now.ToString("yyyyMMddHHmmssffff") & "-" & objAutoNumber.getNextKeyNo("batch_code", "batchAutoNumber") '批次号
        txtBatchNo.Text = batch_no
    End Sub
    Private Sub setEmptyLOC()
        Dim objMstList As New clsMstList
        Dim dt As DataTable
        loc.Items.Clear()
        dt = objMstList.getEmptyLoc("1")
        If dt.Rows.Count > 0 Then
            For i As Integer = 0 To dt.Rows.Count - 1
                loc.Items.Add(dt.Rows(i)("mst_value"))
            Next
        End If
    End Sub
    Private Sub save(sender As Object, e As EventArgs) Handles btnAdd.Click
        '画面项目的非空判断
        '批次
        If chkEmpty(txtBatchNo) = False Then
            Exit Sub
        End If
        If chkEmpty(pwdt) = False Then
            Exit Sub
        End If
        If chkEmptyNum(rk_num) = False Then
            Exit Sub
        End If
        If String.IsNullOrEmpty(cmb_factory.Text) Then
            Exit Sub
        End If
        'If chkEmpty(loc) = False Then
        '    Exit Sub
        'End If
        Dim mst_value_list As String() = cmb_type.Text.Split(" ")
        Dim ty_value As String = mst_value_list(0)
        Dim m_code As String = mst_value_list(2).Replace("(", "").Replace(")", "")
        Dim oper_ty As String = "骨架板"
        If rdo_flb.Checked Then
            oper_ty = "法兰板"
        End If
        dt.Rows.Add(txtBatchNo.Text.Trim, oper_ty, ty_value, m_code, pwdt.Text, rkdt.Text, rk_num.Value, cmb_factory.Text)
        dgv.AutoGenerateColumns = False
        dgv.DataSource = dt
    End Sub

    Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
        If dgv.Rows.Count > 0 Then
            If MsgBox("您确定要进行删除吗？", MsgBoxStyle.OkCancel, "提示") = MsgBoxResult.Ok Then
                dt.Rows.RemoveAt(dgv.SelectedRows(0).Index)
            End If
        End If
    End Sub

    Private Function toNewEn(batch_no As String,
                             oper_type As String,
                             type_value As String,
                             material_code As String,
                             pw_date As String,
                             into_date As String,
                             into_num As String,
                             assist_marker As String) As entitypw_rk
        Dim en As New entitypw_rk
        en.batch_no = batch_no
        If oper_type = "法兰板" Then
            en.oper_type = "1"
        Else
            en.oper_type = "0"
        End If
        en.type_value = type_value
        en.material_code = material_code
        en.pw_date = pw_date
        en.into_date = into_date
        en.into_num = Decimal.Parse(into_num)
        en.assist_marker = assist_marker
        en.location = String.Empty
        en.into_ope = usr
        en.validity = "1"
        Return en
    End Function


    Private Sub btnRK_Click(sender As Object, e As EventArgs) Handles btnRK.Click
        If dt.Rows.Count > 0 Then
            If MsgBox("您确定要进行入库吗？", MsgBoxStyle.OkCancel, "提示") = MsgBoxResult.Ok Then
                Dim code As String = Now.ToString("yyyyMMdd") & "-" & objAutoNumber.getNextKeyNo("pw_rk_code", "pwrkAutoNumber")
                For i = 0 To dt.Rows.Count - 1
                    'MES入库
                    Dim en As entitypw_rk = toNewEn(
                    dt.Rows(i)("batch_no").ToString,
                    dt.Rows(i)("oper_type").ToString,
                    dt.Rows(i)("type_value").ToString,
                    dt.Rows(i)("material_code").ToString,
                    dt.Rows(i)("pw_date").ToString,
                    dt.Rows(i)("into_date").ToString,
                    dt.Rows(i)("into_num").ToString,
                    dt.Rows(i)("assist_marker").ToString)
                    objpw_ck.insert(code, en)
                Next
                objpw_ck.insert(code, usr)
                objAutoNumber.setNextKeyNo("pw_rk_code", "pwrkAutoNumber")
                initdt()
                setForm()
                MsgBox("入库成功")
                CType(Owner, FormMain).setDgvData()
            End If
        End If
    End Sub

    Private Sub rk_num_MouseDown(sender As Object, e As MouseEventArgs) Handles rk_num.MouseDown
        UcDigitalKeyboard1 = New ucDigitalKeyboard
        If sender.MaskBox.Mask.EditMask = "d" Then
            UcDigitalKeyboard1.B_01 = False
        Else
            UcDigitalKeyboard1.B_01 = True
        End If

        If sender.GetType().Name.Equals("TextEdit") Then
            UcDigitalKeyboard1.B_02 = True
        Else
            UcDigitalKeyboard1.B_02 = False
        End If

        UcDigitalKeyboard1.Txt = sender

        Me.Controls.Add(UcDigitalKeyboard1)
        UcDigitalKeyboard1.Show()
        UcDigitalKeyboard1.BringToFront()
        UcDigitalKeyboard1.Focus()
    End Sub

    Private Sub btnMesPlan_Click(sender As Object, e As EventArgs) Handles btnMesPlan.Click
        Dim formMesPlan As New FormMesPlan()
        formMesPlan.ShowDialog(Me)
    End Sub

End Class

Public Class entitypw_rk
    Public factory_id As String   ' 工厂ID
    Public batch_no As String   ' 批次
    Public location As String   ' 库位号
    Public pgd_id As String   ' 派工单号
    Public gjb_type As String   ' 骨架板型号
    Public pw_date As DateTime   ' 抛丸时间
    Public begin_date As DateTime   ' 入库时间
    Public into_date As DateTime   ' 入库时间
    Public status As String   ' 状态
    Public end_date As DateTime   ' 出库时间
    Public into_num As Decimal   ' 入库数量
    Public assist_marker As String  '外协厂家
    Public cur_num As String   ' 当前数量
    Public update_time As String   ' 修改时间
    Public into_ope As String '入库作业员
    Public oper_type As String '操作类型
    Public type_value As String '类型值
    Public material_code As String '物料编码
    Public validity As String
End Class